/////////////////////////////////////////////////////////////////////////////////////////////
// Filename: Sprite.h
// (c) Dave Ottley, 2012. All rights reserved.
/////////////////////////////////////////////////////////////////////////////////////////////
#ifndef SPRITE_H
#define SPRITE_H

///////////////////////////
//		INCLUDES
///////////////////////////
#include <string>
#include <stdint.h>

///////////////////////////
//		MY INCLUDES
///////////////////////////
#include "Rect.h"
#include "D3DClass.h"

///////////////////////////
//		Sprite
///////////////////////////
class Sprite
{
public:
	Sprite(Coord1D fDestUpperLeft, int frameWidthInPixels, int frameHeightInPixels, 
		Point spriteAtlasPositionOnMasterAtlas = Point(0,0), // set to zero if not atlased
		bool atlased = true, 
		const wchar_t * filename = 0, 
		uint32_t numAnimationFrames = 1, 
		uint32_t columns = 1, uint32_t rows = 1, float animationTimeDelta = 0.1f,  
		float scale = 1.0f, uint32_t startingFrame = 0);

	Sprite(const Sprite&) { }

	~Sprite() { ReleaseTexture(); }
	
	inline const void * const GetTexture() const { return mTexture; }

	bool Update(float deltaX = 0.0f, float deltaY = 0.0f, float deltaScale = 0.0f);

	inline void Render() 
	{ 
		if(!mAtlased)
		{
			D3DCls->Draw(mSourceRect, mDestRect, mTexture); 
		}
		else
		{
			D3DCls->DrawAtlased(mSourceRect, mDestRect);
		}
	} 

	bool LoadTexture(std::wstring SpritePath);

private:
	inline void ReleaseTexture() { D3DCls->ReleaseResource(&mTexture); }

private:
	// SIZE AND POSITION
	Rect mSourceRect;
	Rect mDestRect; // must be float (-1,1) range, screen centered on (0,0)
	Point mSpriteSheetUpperLeftPosition; // Position of the sprite sheet on the master atlas (pixel coords)
	float mScale;
	bool mAtlased;	// Is this sprite part of the master atlas?
	void * mTexture; // Image texture buffer, if non-atlased.
	uint32_t mNumAnimationFrames;
	uint32_t mAnimationFrame;
	uint32_t mColumns;
	uint32_t mRows;
	uint32_t mFrameWidth;
	uint32_t mFrameHeight;
	uint32_t mSpriteSheetWidthInPixels;
	uint32_t mSpriteSheetHeightInPixels;
	float mAnimationTimeDelta;

	std::wstring mFilePath;
};

#endif // SPRITE_H